package com.kaka.bus.engine.user.register;

import android.content.Intent;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.kaka.bus.AppCenter;
import com.kaka.bus.R;
import com.kaka.bus.api.Api;
import com.kaka.bus.base.BaseActivity;
import com.kaka.bus.bean.HttpWrapper;
import com.kaka.bus.constant.Constant;
import com.kaka.bus.constant.UserConstant;
import com.kaka.bus.engine.borrow.pact.PactGreementActivity;
import com.kaka.bus.engine.other.DrainageActivity;
import com.kaka.bus.util.U;
import com.kaka.bus.util.logger.Logger;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;

/**
 * Created by zhangqi on 2016/9/28.
 * e-mail : ${email}
 * desc :
 */

public class RegistActivity extends BaseActivity<RegistPresenter> implements RegistContract.View {

    @BindView(R.id.toolbar_back)
    ImageView mToolbarBack;
    @BindView(R.id.toolbar_name)
    TextView mToolbarName;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.edt_regist_phone)
    EditText mEdtRegistPhone;
    @BindView(R.id.tv_regist_send)
    TextView mTvRegistSend;
    @BindView(R.id.edt_regist_put)
    EditText mEdtRegistPut;
    @BindView(R.id.edt_regist_invite)
    EditText mEdtRegistInvite;
    @BindView(R.id.cb_regist_agree)
    CheckBox mCbRegistAgree;
    @BindView(R.id.tv_regist_xieyi)
    TextView mTvRegistXieyi;
    @BindView(R.id.btn_regist)
    AppCompatButton mBtnRegist;
    @BindView(R.id.tv_regist_gologin)
    TextView mTvRegistGologin;

    private String mPhone;

    private String mSmsToken;

    private String mSms;

    private String mSmsPhone;

    private String mSmsCode;

    @Override
    protected void initView() {
        super.initView();
        mToolbarName.setText("注册");
        mToolbarBack.setOnClickListener(v -> finish());
    }

    @Override
    protected void initListener() {
        /**
         * 验证码
         */
        RxView.clicks(mTvRegistSend)
                .subscribe(v -> {
                    phoneIsExist();
                });

        /**
         * 点击下一步
         */
        RxView.clicks(mBtnRegist)
                .throttleFirst(3, TimeUnit.SECONDS)
                .subscribe(v -> regist());

        mTvRegistGologin.setOnClickListener(v -> {
            finish();
        });

        /**
         * 注册协议
         */
        mTvRegistXieyi.setOnClickListener(v -> {
            Intent intent = new Intent(this, PactGreementActivity.class);
            intent.putExtra(PactGreementActivity.TYPE, PactGreementActivity.TYPE_LOAD_URL);
            intent.putExtra(PactGreementActivity.URL, Api.BASE_URL + "loan/platAgreement");
            intent.putExtra(PactGreementActivity.TITLE, "平台服务协议");
            startActivity(intent);
        });
    }

    public void regist() {
        mPhone = mEdtRegistPhone.getText().toString();
        mSms = mEdtRegistPut.getText().toString();
//        String invite = mEdtRegistInvite.getText().toString();

        if (!U.isMobileExact(mPhone)) {
            showMessage(R.string.enterOkPhone);
            return;
        }

        if (TextUtils.isEmpty(mPhone)) {
            showMessage(R.string.phonenotnull);
            return;
        }

        if (TextUtils.isEmpty(mSms)) {
            showMessage("请先填写短信验证码");
            return;
        }
        /**
         * 是否同意协议
         */
        if (!isCheck()) {
            showMessage(R.string.read_);
            return;
        }

        mPresenter.checkInvite(mPhone, mSms);


//
//        if(TextUtils.isEmpty(invite)){
//            toRegister();
//        }else{
//            /**
//             * 校验邀请码
//             */
//            mPresenter.checkInvite(invite);
//        }
    }

    @Override
    public void checkInviteResult(HttpWrapper<String> bean) {
        if (Constant.SUCCESS_CODE.equals(bean.code)) {
            toRegister();
        } else {
            showMessage(bean.getInfo());
        }
    }


    private void toRegister() {
        Intent intent = new Intent(this, RegistPwdActivity.class);
        intent.putExtra(UserConstant.REGIST_PHONE, mPhone);
        intent.putExtra(RegistPwdActivity.VERIFYCODE, getText(mEdtRegistPut));
        startActivity(intent);
    }


    public boolean sendyzm() {
        mPhone = getText(mEdtRegistPhone);
        mSmsPhone = mPhone;
        if (!U.isMobileExact(mPhone)) {
            showMessage("请输入正确的手机号");
            return true;
        }

        return mPresenter.sendyzm(mPhone, AppCenter.getTokenKey());
    }

    @Override
    public void phoneState(HttpWrapper<String> wrapper) {
        if (Constant.SUCCESS_CODE.equals(wrapper.code)) {
            sendyzm();
        } else if (Constant.ERROR_CODE.equals(wrapper.code)) {
            showMessage(wrapper.info);
        } else {
            showMessage(wrapper.info);
        }
    }


    @Override
    public boolean isCheck() {
        return mCbRegistAgree.isChecked();
    }

    @Override
    public void phoneIsExist() {
        mPhone = mEdtRegistPhone.getText().toString();
        if (!U.isMobileExact(mPhone)) {
            showMessage("请输入正确的手机号");
            return;
        }
        mPresenter.isExist(mPhone);
    }

    @Override
    public void sendSmsSuccess(HttpWrapper<String> bean) {
        if (Constant.SUCCESS_CODE.equals(bean.getCode())) {
            time();
        } else if (Constant.BQSLOSE_CODE.equals(bean.getCode())) {
            U.toDrainage(this, DrainageActivity.TYPE_REFUSE,Constant.REFUES);
        } else {
            showMessage(bean.getInfo());
        }

    }

    @Override
    public void time() {
        showMessage("成功发送验证码");
        Logger.i("验证码倒计时开始");
        mTvRegistSend.setEnabled(false);
        addSubscription(U.countdownFlowable(59)            //倒计时60秒
                .subscribe(
                        time -> mTvRegistSend.setText("重新发送" + "(" + time + ")"),    //每秒赋值
                        U::errorUtil,             //提示错误信息
                        () -> {
                            mTvRegistSend.setEnabled(true);
                            mTvRegistSend.setText(getString(R.string.reSendyzm));
                        }));
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_regist;
    }

    @Override
    protected boolean needFullScreen() {
        return true;
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

}
